package com.im.partchat.topic.widget;

import android.content.Context;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.bumptech.glide.Glide;
import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.topic.BaseActivity;
import com.im.partchat.topic.MenuDialog;
import com.im.partchat.topic.bean.AvatarBean;
import com.im.partchat.topic.bean.CommentBean;
import com.im.partchat.topic.bean.TopicBean;
import com.im.partchat.topic.manager.FaceManager;

public class TopicDetailHeaderLayout extends LinearLayout {
	public TextView share_count_btn;
	public PraiseTextView zan_count_btn;
	private TextView praise_count_textview;
	private BaseActivity context;
	public TopicDetailHeaderLayout(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.layout_topic, this, true);
	}

	public void initView(final BaseActivity context, final TopicBean bean) {
		this.context = context;
		ImageView avator = (ImageView) findViewById(R.id.avator);
		TextView header_time = (TextView) findViewById(R.id.header_time);
		TextView content = (TextView) findViewById(R.id.content);
		TextView header_name = (TextView) findViewById(R.id.header_name);
		TextView header_location = (TextView)  findViewById(R.id.header_location);
		PictureGridLayout pictureGridLayout = (PictureGridLayout) findViewById(R.id.pictureGridLayout);
		
		View bottom_praise_ll = findViewById(R.id.bottom_praise_ll);
		praise_count_textview = (TextView)findViewById(R.id.praise_count_textview);
		zan_count_btn = (PraiseTextView) findViewById(R.id.zan_count_btn);
		
		share_count_btn = (TextView) findViewById(R.id.share_count_btn);
		Glide.with(App.getApplication())
				.load(bean.getMemberAvatar().findSmallUrl())
				.error(R.drawable.rc_image_default)
				.into(avator);
		content.setText(App.getFaceManager().
				convertNormalStringToSpannableString(context,bean.getContent()),
				BufferType.SPANNABLE);
		FaceManager.extractMention2Link(content);
		content.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				MenuDialog followDialog = new MenuDialog(context,
						new MenuDialog.ButtonClick() {

							@Override
							public void onSureButtonClick() {
								// TODO Auto-generated method stub
								ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
								clipboard.setText(bean.getContent());
							}
						});
				followDialog.show();
				return true;
			}
		});
		header_location.setText(bean.getAddress());
		header_name.setText(bean.getMemberName());
		header_time.setText(bean.getTimeString());
		
		pictureGridLayout.setPictures(context, bean.getPicture(), bean);
		
		praise_count_textview.setText(bean.getPraiseCount()+"人点赞");
		zan_count_btn.setPraiseState(context, bean);
		findViewById(R.id.praise_ll).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PraiseTextView tv =  (PraiseTextView) v.findViewById(R.id.zan_count_btn);
				tv.clickPraise(context, bean);
			}
		});
		
		SpannableTextView bottom_textview = (SpannableTextView) findViewById(R.id.bottom_textview);
		bottom_textview.setPraiseText(	bean.getPraiseMembers(), new SpannableTextView.MemberClickListener() {
			
			@Override
			public void onMemberClick(CommentBean commentBean) {
				// TODO Auto-generated method stub
			}
		});
		bottom_praise_ll.setVisibility(bean.getPraiseMembers().isEmpty()?View.GONE:View.VISIBLE);
	
		avator.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			}
		});
	}
	

	//重新设置点赞状态
	public void setPraiseStatus( final TopicBean bean){
		zan_count_btn.setPraiseState(context, bean);

		boolean contains = false;
		for (CommentBean praiseMember : bean.getPraiseMembers()) {
			if (praiseMember.getMemberId().equals(App.getUserBean().getId())) {
				 bean.getPraiseMembers().remove(praiseMember);
				 contains = true;
				 break;
			}
		}
		if (!contains) {
			CommentBean newBean = new CommentBean();
			newBean.setMemberId(App.getUserBean().getId());
			AvatarBean avatarBean = new AvatarBean();
			avatarBean.setOriginal(App.getUserBean().getHeadUrl());
			avatarBean.setSmall(App.getUserBean().getHeadUrl());
			newBean.setMemberAvatar(avatarBean);
			newBean.setMemberName(App.getUserBean().getName());
			newBean.setContent("");
			bean.getPraiseMembers().add(newBean);
		}
		SpannableTextView bottom_textview = (SpannableTextView) findViewById(R.id.bottom_textview);
		bottom_textview.setPraiseText(	bean.getPraiseMembers(), new SpannableTextView.MemberClickListener() {
			
			@Override
			public void onMemberClick(CommentBean commentBean) {
				// TODO Auto-generated method stub
			}
		});
		View bottom_praise_ll = findViewById(R.id.bottom_praise_ll);
		bottom_praise_ll.setVisibility(bean.getPraiseMembers().isEmpty()?View.GONE:View.VISIBLE);
		praise_count_textview.setText(""+bean.getPraiseCount()+"人点赞");
	
	}
	

}
